package com.study.example.study.extension.easyexcel.write;

import cn.hutool.core.util.PageUtil;

import java.util.Objects;

/**
 * 分页写数据工具类
 *
 * @author RenYuLiang
 */
public final class EasyExcelWritePageUtils {

    /**
     * 禁止实例化
     */
    private EasyExcelWritePageUtils() {

    }

    /**
     * 创建  EasyExcelWritePageDTO
     *
     * @param pageSize     每页查询多少条
     * @param maxFindCount 最大查询多少条
     * @param totalCount   总数
     * @return EasyExcelWritePageDTO
     */
    public static EasyExcelWritePageDTO createEasyExcelWritePageDTO(Integer pageSize, Long maxFindCount, Long totalCount) {
        EasyExcelWritePageDTO dto = new EasyExcelWritePageDTO();
        dto.setPageNo(0);
        dto.setPageSize(pageSize);
        dto.setTotalCount(0L);
        dto.setTotalPage(0L);
        dto.setMaxCount((Objects.isNull(maxFindCount) || 0L > maxFindCount) ? 0L : maxFindCount);
        dto.setHasData(false);
        if (Objects.isNull(totalCount) || 1L > totalCount) {
            return dto;
        }

        // 计算最大数量
        long maxCount = totalCount;
        if (0L < dto.getMaxCount() && totalCount > dto.getMaxCount()) {
            maxCount = dto.getMaxCount();
        }
        // 总页数
        long totalPage = PageUtil.totalPage(maxCount, dto.getPageSize());
        dto.setTotalCount(maxCount);
        dto.setTotalPage(totalPage);
        dto.setHasData(true);
        return dto;
    }

}
